package com.ywq.app.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ywq.app.biz.AuxContentBiz;
import com.ywq.app.biz.MailBiz;
import com.ywq.app.dao.AuxContentDao;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.domain.ArtReminder;
import com.ywq.app.domain.Channel;
import com.ywq.app.domain.Content;
import com.ywq.app.domain.Member;
import com.ywq.common.constant.PageSize;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;
import com.ywq.common.util.IdGenerator;



public class AuxContentBizImpl implements  AuxContentBiz {

	private final static Logger logger = LoggerFactory.getLogger(AuxContentBizImpl.class);
	
	private AuxContentDao auxContentDao;

	public void setAuxContentDao(AuxContentDao auxContentDao) {
		this.auxContentDao = auxContentDao;
	}
	@Autowired
	private MailBiz mailBiz;
	/**
	 * 首页内容
	 * @按频道mark（父类频道所有）
	 */
	@Override
	public PageModel getIndexListByChannel(Integer page,String identifier) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_ADMIN);
		map.put("pageSize", PageSize.PAGE_SIZE_ADMIN);
		//mark得到父频道标识
		map.put("parent", identifier);
		int count = auxContentDao.getIndexListCountByChannel(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(auxContentDao.getIndexListByChannel(map));
		pm.setPageSize(PageSize.PAGE_SIZE_ADMIN);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_ADMIN - 1)
				/ PageSize.PAGE_SIZE_ADMIN);
		return pm;
	}
	/**
	 * 
	 * @param channel
	 * @throws AppBusinessException
	 */
	@Override
	public void insertChannel(Channel channel) throws AppBusinessException {
		String id = IdGenerator.createIdentifier();
		channel.setIdentifier(id);
		auxContentDao.insertChannel(channel);
	}
	/**
	 * 取得父类id
	 */
	@Override
	public String getIdentifierByMark(String mark)throws AppBusinessException {
		String identifier=auxContentDao.getPartentByMark(mark);
		return identifier;
	}
	/**
	 * 根据Id查询某一频道
	 * getChannelById
	 */
	@Override
	public Channel getChannelById(String id)throws AppBusinessException {
		Channel channel =auxContentDao.getChannelById(id);
		return channel;
	}
	/**
	 * 0728
	 */
	@Override
	public Channel getChannelByMark(String mark)throws AppBusinessException {
		Channel channel =auxContentDao.getChannelByMark(mark);
		return channel;
	}
	
	/**
	 * 更新频道
	 */
	@Override
	public void updateChannel(Channel channel) throws AppBusinessException {
		auxContentDao.updateChannel(channel);
	}
	/**
	 * 删除频道
	 */
	@Override
	public void deleteChannel(String identifier) throws AppBusinessException {
		auxContentDao.deleteChannel(identifier);
	}
	/**
	 * 删除内容
	 */
	@Override
	public void deleteContent(String identifier) throws AppBusinessException {
		auxContentDao.deleteContent(identifier);
	}
	/**
	 * 内容列表
	 */
	@Override
	public PageModel getChannelContentListById(Integer page,String channel) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * PageSize.PAGE_SIZE_ADMIN);
		map.put("pageSize", PageSize.PAGE_SIZE_ADMIN);
		//mark得到父频道标识
		map.put("channel", channel);
		int count = auxContentDao.getChannelContentListCountById(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(auxContentDao.getChannelContentListById(map));
		pm.setPageSize(PageSize.PAGE_SIZE_ADMIN);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + PageSize.PAGE_SIZE_ADMIN - 1)
				/ PageSize.PAGE_SIZE_ADMIN);
		return pm;
	}
	/**
	 * 增加内容
	 */
	@Override
	public void insertContent(Content content) throws AppBusinessException {
		//content中的标识
		String id = IdGenerator.createIdentifier();
		content.setIdentifier(id);
		auxContentDao.insertContent(content);
	}
	/**
	 * 通过mark得到频道号，再查取频道的列表
	 */
	@Override
	public List<Content> getRoomByChannel(String mark,Integer num) throws AppBusinessException {
		//得到频道号
		String identifier=auxContentDao.getPartentByMark(mark);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", identifier);
		map.put("pageSize", num);
		List<Content> roomLBContent=auxContentDao.getChannelContentListById(map);
		return roomLBContent;
		//content中的标识
	}
	/**
	 * 通过艺术家ID得到故事内容
	 */
	@Override
	public Content getStoryByArtistId(String id) throws AppBusinessException {
		//得到频道号
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("channel", id);
		map.put("type",PageSize.MADE_STORY );
		Content story=auxContentDao.getStoryByArtistId(map);
		return story;
	}
	@Override
	public Content getContentId(String id) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("identifier", id);
		Content content=auxContentDao.getStoryByArtistId(map);
		return content;
	}
	//更新内容
	@Override
	public void updateContent(Content content) throws AppBusinessException {
		auxContentDao.updateContent(content);
	}
	//邮箱列表
	@Override
	public PageModel getArtEmailGrid(Integer page,Integer pageSize,String type) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pageSize);
		map.put("pageSize", pageSize);
		map.put("type", type);
		int count = auxContentDao.getArtEmailCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(auxContentDao.getArtEmailList(map));
		pm.setPageSize(pageSize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pageSize - 1)
				/ pageSize);
		return pm;
	}
	@Override
	public void updateArtReminder(ArtReminder artReminder) throws AppBusinessException {
		auxContentDao.updateArtReminder(artReminder);
	}
	//发送邮件
	@Override
	public void sendEmail(Map<String, Object> map) throws AppBusinessException {
		map.put("enabled", "true");
		List<ArtReminder>  artReminderList=  auxContentDao.getArtEmailList(map);
		for(ArtReminder artReminder :artReminderList){
			Member member =new Member();
			member.setEmail(artReminder.getEmail());
			mailBiz.sendEmail("http://mkhg.v084.10000net.cn",  member, "笔飞商城订阅邮件", (String)map.get("description"));
		}
		
	}
	
	
}
